package com.chaoge.web.controller.store;

import com.chaoge.domain.store.QuestionItem;
import com.chaoge.utils.BeanUtil;
import com.chaoge.web.controller.BaseServlet;
import com.github.pagehelper.PageInfo;
import org.apache.commons.lang3.StringUtils;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
@WebServlet("/store/questionItem")
public class QuestionItemServlet extends BaseServlet {
    //private QuestionItemService questionItemService=new QuestionItemServiceImpl();
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String operation = request.getParameter("operation");
        switch (operation){
            case "list":
                this.list(request,response);
                break;
            case "save":
                this.save(request,response);
                break;
            case "toEdit":
                this.toEdit(request,response);
                break;
            case "edit":
                this.edit(request,response);
                break;
            case "delete":
                this.delete(request,response);
                break;
        }
    }

    private void delete(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {
        //获取数据
        QuestionItem questionItem= BeanUtil.fillBean(request,QuestionItem.class);
        //将数据传到questionItemService层
        questionItemService.delete(questionItem);
        //跳转到List页面
        list(request,response);
    }

    private void edit(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {
        //获取数据
        QuestionItem questionItem= BeanUtil.fillBean(request,QuestionItem.class,"yyyy-MM-dd");
        //将数据传到questionItemService层
        questionItemService.update(questionItem);
        //跳转到List页面
        list(request,response);
    }

    private void toEdit(HttpServletRequest request, HttpServletResponse response)  throws ServletException, IOException  {
        //获取id
        String id = request.getParameter("id");
        //查询单个数据
        QuestionItem questionItem = questionItemService.findById(id);
        //设置数据
        request.setAttribute("questionItem",questionItem);
        request.setAttribute("operation","edit");
        //跳转到List页面
        list(request,response);
    }

    private void save(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException  {
        //获取数据
        QuestionItem questionItem= BeanUtil.fillBean(request,QuestionItem.class,"yyyy-MM-dd");
        //将数据传到questionItemService层
        questionItemService.save(questionItem);
        //跳转到List页面
        list(request,response);
    }

    private void list(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String questionId=request.getParameter("questionId");
        request.setAttribute("questionId",questionId);
        //进入列表页
        PageInfo all = questionItemService.findAll(questionId,1, 100);
        //将数据保存
        request.setAttribute("page",all);
        if (request.getAttribute("operation")==null){
            request.setAttribute("operation","save");
        }
        //页面跳转
        request.getRequestDispatcher("/WEB-INF/pages/store/questionItem/list.jsp").forward(request,response);
    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        this.doGet(request, response);
    }

}
